package fun.ticsmyc.sort;

import fun.ticsmyc.tools.ArrayTools;
import org.junit.Test;

import java.util.Arrays;

/**
 * 冒泡排序
 *
 * @author Ticsmyc
 * @package fun.ticsmyc.sort1
 * @date 2020-01-30 16:54
 */
public class BubbleSort {
    public static void bubbleSort(int[] arr){

        boolean sorted = true;
        for(int i=0;i<arr.length;++i){
            for(int j =0 ;j<arr.length-i-1;++j){
                if(arr[j]>arr[j+1]){
                    ArrayTools.swap(arr,j,j+1);
                    sorted = false;
                }
            }
            if(sorted){
                break;
            }
            sorted=true;
        }
    }

    @Test
    public void test(){

        //测试次数
        int testTime=5_000_000;
        //数组大小
        int size=10;
        //数组值 -100 ~ +100
        int value=100;
        boolean succeed =true;

        for(int i=0;i<testTime;++i){
            int[] arr1 = ArrayTools.generateRandomArray(size,value);

            int[] arr2 = Arrays.copyOf(arr1,arr1.length);
            int[] arr3 = Arrays.copyOf(arr1,arr1.length);

            bubbleSort(arr1);
            ArrayTools.rightSortMethod(arr2);
            if(!Arrays.equals(arr1,arr2)){
                succeed=false;
                //打印原数组
                ArrayTools.printArray(arr3);
                break;
            }
        }
        System.out.println( succeed ? "nice" : "Fucking fucked!");
    }

    @Test
    public void test2(){
        int[] arr = new int[]{16,81,5,-12,61};
        ArrayTools.printArray(arr);
        bubbleSort(arr);
    }
}
